package com.gnerv.cms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gnerv.cms.dto.ArticleDTO;
import com.gnerv.cms.dto.ArticleSearchDTO;
import com.gnerv.cms.service.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * CMS-文章表 前端控制器
 * </p>
 *
 * @author Gnerv LiGen
 * @since 2019-10-10
 */
@Api(tags = "CMS管理")
@Slf4j
@CrossOrigin
@Controller
@RequestMapping("/api/cms")
public class CmsController {

    @Resource
    private IArticleService articleService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String index(ModelMap modelMap) {
        return "default/dark/index";
    }

    @RequestMapping(value = "/catalog/{catalogId}", method = RequestMethod.GET)
    public String catalogList(@PathVariable(value = "catalogId") String catalogId, ModelMap modelMap) {
        modelMap.addAttribute("CatalogId", catalogId);
        return "default/dark/catalogList";
    }

    @RequestMapping(value = "/catalog/article/list/{catalogId}", method = RequestMethod.GET)
    public String listCatalogArticle(@PathVariable(value = "catalogId") String catalogId, ModelMap modelMap) {
        modelMap.addAttribute("CatalogId", catalogId);
        return "default/dark/articleList";
    }

    @RequestMapping(value = "/article/list/{title}", method = RequestMethod.GET)
    public String listArticle(@PathVariable(value = "title") String title, ModelMap modelMap) {
        ArticleSearchDTO articleSearchDTO = new ArticleSearchDTO();
        articleSearchDTO.setTitle(title);
        Page<ArticleDTO> articles = articleService.listArticle(articleSearchDTO, 1L, 30L);
        List<ArticleDTO> records = articles.getRecords();
        modelMap.addAttribute("articles", records);
        return "default/dark/articleSearchList";
    }

    @RequestMapping(value = "/article/{articleId}", method = RequestMethod.GET)
    public String article(@PathVariable(value = "articleId") String articleId, ModelMap modelMap) {
        modelMap.addAttribute("ArticleId", articleId);
        return "default/dark/articleDetail";
    }

    private String getTemplatePath(String type){
        return "";
    }

}
